package org.holynite.fieldmanage.managed.backing

import org.holynite.fieldmanage.managed.model.AdministratorFormBean
import org.holynite.fieldmanage.managed.model.AdministratorsBean

import javax.faces.bean.ManagedBean
import javax.faces.bean.ManagedProperty
import javax.faces.bean.RequestScoped
import javax.faces.event.ValueChangeEvent

/**
 * Created by midnite on 15/1/31.
 */
@ManagedBean
@RequestScoped
class AdministratorSelectBean implements Serializable {

    @ManagedProperty("#{administratorFormBean}")
    AdministratorFormBean adminForm
    @ManagedProperty("#{administratorsBean}")
    AdministratorsBean adminsBean

    void loadAdminDetail(ValueChangeEvent event) {
        def selected = event.newValue as String
        if (selected.equals("new_content")) {
            adminForm.addMode = true
            adminForm.clearForm()
        } else {
            adminForm.addMode = false
            def admin = adminsBean.findAdministrator(selected)
            adminForm.adminWorkId = admin.workdId
            adminForm.adminName = admin.name
            adminForm.adminDisabled = admin.disabled
            adminForm.selectedRole = admin.correspondRole
        }
    }
}
